<template>
    <div id="new-question">
        <group class="question-content">
            <x-textarea class="title-question" :title="'标题'" :placeholder="'标题清晰更能吸引他人的注意哦~(5-50字以内)'" v-model="titleval" :max="50" :show-counter="false" :rows="1" autosize></x-textarea>
            <x-textarea class="desc-question" :title="'描述'" :placeholder="'描述清楚的提问更容易被人回答哦~（800字以内）'" v-model="contentval" :max="800" :show-counter="false" :height="200" :rows="8" :cols="30"></x-textarea>
        </group>
        <upload-card :type="2"><p>仅支持：JPEG、GIF、PNG</p></upload-card>
        <div class="share" @click="release">发布</div>
    </div>
</template>
<script>
var timeStamp = String(Number(new Date()))
import { XTextarea, Group, Flexbox, FlexboxItem, XButton } from 'vux'
import UploadCard from '../../comment/UploadCard'
import api from '../../api'
import utils from '../../utils'
export default {
  data () {
    return {
      titleval: '',
      contentval: '',
      tokenval: ''
    }
  },
  components: {
    XTextarea,
    Group,
    Flexbox,
    FlexboxItem,
    XButton,
    UploadCard
  },
  created () {
    window.document.title = '发布'
    this.tokenval = utils.AuthToken(timeStamp, this.$store.state.userkey)
    this.$store.commit('changeIndexConf', {
      uploadimg: ''
    })
    utils.jssdkwx(api, this.$wechat, this.$store)
  },
  methods: {
    release () {
      if (this.titleval === '') {
        this.$vux.toast.show({
          text: '标题不能为空',
          type: 'warn',
          width: '4rem'
        })
      } else if (this.titleval.length < 5 || this.titleval.length > 50) {
        this.$vux.toast.show({
          text: '标题在5-50个字以内',
          type: 'warn',
          width: '4rem'
        })
      } else if (this.contentval === '') {
        this.$vux.toast.show({
          text: '内容不能为空',
          type: 'warn',
          width: '4rem'
        })
      } else if (this.contentval.length === 800) {
        this.$vux.toast.show({
          text: '内容在800个字以内',
          type: 'warn',
          width: '4rem'
        })
      } else {
        api.issue({
          token: this.tokenval,
          timestamp: timeStamp,
          userId: this.$store.state.userID,
          title: this.titleval,
          content: this.contentval,
          picUrl: this.$store.state.uploadimg
        }).then(res => {
          console.log(res)
          if (res.status === 200) {
            this.$router.push('/circle/index')
          } else {
            this.$vux.toast.show({
              text: res.message,
              type: 'warn',
              width: '4rem'
            })
          }
        })
      }
    }
  }
}
</script>
